package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.PositionDto;
import com.example.entity.*;
import com.example.mapper.PositionMapper;
import com.example.service.DPService;
import com.example.service.DepartmentService;
import com.example.service.PositionService;
import com.example.service.UserAService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {

    @Autowired
    private DPService dpService;


    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private UserAService userAService;

    /**
     * 还要保存dp表
     *
     * @param positionDto
     */
    @Override
    public void saveWithDP(PositionDto positionDto) {
        this.save(positionDto);

        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.eq(Department::getId, positionDto.getDepartmentId());

        positionDto.setDepartmentName(departmentService.getOne(departmentLambdaQueryWrapper).getName());

        DepartmentPosition departmentPosition = new DepartmentPosition();
        BeanUtils.copyProperties(positionDto, departmentPosition);

        long orderId = IdWorker.getId();
        departmentPosition.setId(orderId);

        departmentPosition.setPositionId(positionDto.getId());

        dpService.save(departmentPosition);
    }

    @Override
    public void updateByIdWithDP(PositionDto positionDto) {
        this.updateById(positionDto);

        LambdaQueryWrapper<DepartmentPosition> departmentPositionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentPositionLambdaQueryWrapper.eq(DepartmentPosition::getPositionId, positionDto.getId());
        DepartmentPosition one = dpService.getOne(departmentPositionLambdaQueryWrapper);

        one.setDepartmentId(positionDto.getDepartmentId());

        dpService.updateById(one);

    }

    @Override
    public PositionDto getByIdWithDP(Long id) {
        Position position = this.getById(id);

        PositionDto positionDto = new PositionDto();
        BeanUtils.copyProperties(position, positionDto);

        LambdaQueryWrapper<DepartmentPosition> departmentPositionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentPositionLambdaQueryWrapper.eq(DepartmentPosition::getPositionId, id);
        DepartmentPosition one = dpService.getOne(departmentPositionLambdaQueryWrapper);

        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.eq(Department::getId, one.getDepartmentId());

        positionDto.setDepartmentId(one.getDepartmentId());
        positionDto.setDepartmentName(departmentService.getOne(departmentLambdaQueryWrapper).getName());


        log.info("{}", positionDto);

        return positionDto;
    }

    @Override
    public void removeByIdWithDP(Long id) {
        this.removeById(id);

        LambdaQueryWrapper<DepartmentPosition> departmentPositionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentPositionLambdaQueryWrapper.eq(DepartmentPosition::getPositionId, id);


        dpService.removeById(dpService.getOne(departmentPositionLambdaQueryWrapper).getId());


    }
}
